// pages/auth/upload_img.js
const app = getApp();
const _request = require("../../utils/request.js");
require;
Page({
    data: {
        upload3: [],
    },
    onLoad: function (options) {
        if(options.img){
            this.setData({
                upload3:JSON.parse(options.img)
            })
        }
    },
    onUnload: function () {
        var that = this;
        var pages = getCurrentPages(); 
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
            upload3: that.data.upload3
        })
    },
    remove(e){
        var list = this.data.upload3
        list.splice(e.currentTarget.dataset.index, 1)
        this.setData({ upload3:list })
    },
    afterRead(e) {
        var index = e.currentTarget.dataset.index
        let that = this
        wx.chooseImage({
            count: 1,
            sourceType: ['album', 'camera'],
            success(res) {
                if (index < 0) {
                    var list = []
                    list = that.data.upload3.concat(res.tempFilePaths)
                } else {
                    var list = that.data.upload3
                    list.splice(index, 1, res.tempFilePaths[0])
                }
                that.setData({
                    upload3: list
                })
                var tempFilePaths = res.tempFilePaths[0]
                var self = that
                wx.uploadFile({
                    url: app.globalData.baseurl + '/store-api/file/batchUpload',
                    filePath: tempFilePaths,
                    name: 'files',
                    header: {
                        'token': wx.getStorageSync('token')
                    },
                    formData: {
                        'files': 'file'
                    },
                    success(res) {
                        var data = JSON.parse(res.data)
                        if (data.code == 200) {
                            let arr = self.data.upload3
                            if (index < 0) {
                                arr.splice(self.data.upload3.length - 1, 1, data.data[0])
                            } else {
                                arr.splice(index, 1, data.data[0])
                            }
                            self.setData({
                                upload3: arr
                            })
                            console.log(self.data.upload3)
                        } else {
                            wx.showToast({
                                title: '上传失败',
                                icon: 'none'
                            })
                            let arr = self.data.upload3
                            if (index < 0) {
                                arr.splice(self.data.upload3.length - 1, 1)
                            } else {
                                arr.splice(index, 1)
                            }
                            self.setData({
                                upload3: arr
                            })
                        }
                    },
                    fail(error) {
                        wx.showToast({
                            title: '上传失败',
                            icon: 'none'
                        })
                        let arr = self.data.upload3
                        if (index < 0) {
                            arr.splice(self.data.upload3.length - 1, 1)
                        } else {
                            arr.splice(index, 1)
                        }
                        self.setData({
                            upload3: arr
                        })
                    },
                })
            }
        })
    },
})